/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fss.client.ui;

import fss.client.RemoteObjectListItem;
import fss.remote.util.RemoteFileService;
import fss.util.rmi.RmiId;
import fss.util.rmi.RmiUtils;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author hieu
 */
public class CustomList extends JList implements Serializable
{
  DefaultListModel mModel;
  public CustomList(DefaultListModel aModel)
  {
    super(aModel);
    mModel = aModel;
  }

  @Override
  public String getToolTipText(MouseEvent aEvent)
  {
    String tToolTip = "";
    int tIdx = locationToIndex(aEvent.getPoint());
    try
    {
      Object tTest = getModel().getElementAt(tIdx);
      if (tTest instanceof RemoteObjectListItem)
      {
        RemoteObjectListItem tItem = (RemoteObjectListItem) tTest;
        if (tItem.getClassName().equals("Service"))
        {
          RmiId tId = new RmiId(tItem.getHost(), tItem.getName(), tItem.getPort());
          RemoteFileService tService =
            (RemoteFileService) RmiUtils.getRemoteObject(tId);
          tToolTip = "Current Load => Upload: "
            + tService.getUploadCount()
            + ", Download: "
            + tService.getDownloadCount();
        }
      }
      else if (tTest instanceof FileListItem)
      {
        FileListItem tItem = (FileListItem) tTest;
        tToolTip = tItem.getProgress();
      }
    }
    catch (RemoteException aEx)
    {
      mModel.removeElementAt(tIdx);
    }
    // Return the tool tip text
    return tToolTip;
  }
}
